local sk__meihuo = fk.CreateSkill {

  name = "sk__meihuo",

  tags = {},

}



sk__meihuo:addEffect("active", {
  name = "sk__meihuo",
  anim_type = "control",
  min_card_num = 1,
  target_num = 1,
  prompt = "#sk__meihuo",
  can_use = function(self, player)
    return player:usedSkillTimes(sk__meihuo.name, Player.HistoryPhase) == 0
  end,
  card_filter = function(self, player, to_select, selected)
    return Fk:currentRoom():getCardArea(to_select) == Card.PlayerHand
  end,
  target_filter = function(self, player, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= player and to_select:isMale()
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    room:moveCardTo(effect.cards, Card.PlayerHand, target, fk.ReasonGive, sk__meihuo.name, nil, false, player.id)
    if player.dead or target.dead or target:isKongcheng() then return end
    local tos = room:askForChoosePlayers(player, table.map(room:getOtherPlayers(target), Util.IdMapper), 1, 999,
      "#sk__meihuo-choose::" .. target.id, sk__meihuo.name, false)
    tos = table.map(tos, function(id) return room:getPlayerById(id) end)
    room:sortByAction(tos)
    local cards = table.simpleClone(target:getCardIds("h"))
    while not target.dead do
      cards = table.filter(cards, function(id)
        local card = Fk:getCardById(id)
        return (card.trueName == "slash" or card:isCommonTrick()) and table.contains(target:getCardIds("h"), id)
      end)
      if #cards == 0 or target.dead then return end
      room:delay(1000)
      local card = Fk:getCardById(table.random(cards))
      if card.skill:getMinTargetNum(target) == 2 or not target:canUse(card) then
        room:moveCardTo(card, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, sk__meihuo.name, nil, true, target)
      else
        tos = table.filter(tos, function(id)
          return not id.dead
        end)
        if #tos == 0 then
          room:moveCardTo(card, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, sk__meihuo.name, nil, true, target)
        else
          room:useCard({
            from = target,
            tos = tos,
            card = card,
            extraUse = true,
          })
        end
      end
    end
  end,
})

return sk__meihuo
